using System;
using System.Collections;
using System.Web;
using log4net;

namespace Sedna.Core.Controller.Common
{
	/// <summary>
	/// Summary description for LogContextAction.
	/// </summary>
	public class LogExecutionContextAction : AbstractAction
	{
		private static ILog log = LogManager.GetLogger(typeof(LogExecutionContextAction));

		public override IActionResult Execute(IExecutionContext executionContext)
		{
			if(executionContext == null)
			{
				log.Debug("executionContext is null");
			}

			log.Debug("executionContext:");
			for(IDictionaryEnumerator en = executionContext.GetEnumerator(); en.MoveNext();)
			{
				try
				{
					IList list = en.Value as IList;
					if(list != null)
					{
						log.Debug(String.Format("{0}={1}", en.Key, ListToString(list)));
					}
					else
					{
						log.Debug(String.Format("{0}={1}", en.Key, en.Value));
					}
				}
				catch(Exception ex)
				{
					log.Debug("unable to output executionContext under the key='" + en.Key + "'", ex);
				}
			}
			return CreateResult(successStatus);
		}

		private string ListToString(IList list)
		{
			string result = "{";
			foreach(object value in list)
			{
				result += value + ";";
			}
			return list.GetType() + result + "}";
		}
	}
}
